package gui;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import common.CommonMethods;

import java.awt.Font;
import java.awt.Color;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class ServerLog extends JFrame
{
	private JTextArea textArea;
	public BufferedWriter logFile;
	
	public ServerLog() {
		getContentPane().setLayout(null);
		
		JLabel lblServerLog = new JLabel("Server Log");
		lblServerLog.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblServerLog.setBounds(281, 21, 118, 33);
		getContentPane().add(lblServerLog);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(36, 65, 606, 363);
		getContentPane().add(scrollPane);
		
		textArea = new JTextArea();
		textArea.setBackground(Color.BLACK);
		textArea.setForeground(Color.GREEN);
		textArea.setFont(new Font("Monospaced", Font.PLAIN, 13));
		scrollPane.setViewportView(textArea);
		textArea.setEditable(false);
		
		logFile = createFile();
		
		textArea.setText("=========================Beging logging=========================\n");
	}
	
	
	public void addString (String string)
	{		
		String line = new String (CommonMethods.getDateTime() + " " + string);
		textArea.setText(textArea.getText() + "\n" + line);
		try
		{
			this.logFile.write("\n" + line);
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
	
	private BufferedWriter createFile()
	{
		SimpleDateFormat DateFormat = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
		DateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		String filename = DateFormat.format(new Date());
		
		BufferedWriter output = null;
		 File file = new File(filename+".txt");
         try {
			output = new BufferedWriter(new FileWriter(file));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
         
         return output;
		


	
	}
}
